package androidx.appcompat.widget;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import o.ah$h;
import o.ah$i;
import o.ah$j;
import o.ah.e;
import o.ah.g;
import o.bt;
import o.bz;
import o.cm;
import o.cw;
import o.hd;
import o.in;

@RestrictTo({d.LIBRARY_GROUP})
public class ActivityChooserView extends ViewGroup {
    private final c ʻ;
    private boolean ʻॱ;
    int ʼ;
    boolean ʽ;
    final FrameLayout ˊ;
    private final ImageView ˊॱ;
    hd ˋ;
    private final ImageView ˋॱ;
    final DataSetObserver ˎ;
    final FrameLayout ˏ;
    private final int ˏॱ;
    private final OnGlobalLayoutListener ͺ;
    final ActivityChooserViewAdapter ॱ;
    private final Drawable ॱˊ;
    private int ॱˋ;
    OnDismissListener ॱॱ;
    private final View ᐝ;
    private ListPopupWindow ᐝॱ;

    class ActivityChooserViewAdapter extends BaseAdapter {
        private static final int ITEM_VIEW_TYPE_ACTIVITY = 0;
        private static final int ITEM_VIEW_TYPE_COUNT = 3;
        private static final int ITEM_VIEW_TYPE_FOOTER = 1;
        public static final int MAX_ACTIVITY_COUNT_DEFAULT = 4;
        public static final int MAX_ACTIVITY_COUNT_UNLIMITED = Integer.MAX_VALUE;
        private bz mDataModel;
        private boolean mHighlightDefaultActivity;
        private int mMaxActivityCount = 4;
        private boolean mShowDefaultActivity;
        private boolean mShowFooterView;

        ActivityChooserViewAdapter() {
        }

        public void setDataModel(bz bzVar) {
            bz dataModel = ActivityChooserView.this.ॱ.getDataModel();
            if (dataModel != null && ActivityChooserView.this.isShown()) {
                dataModel.unregisterObserver(ActivityChooserView.this.ˎ);
            }
            this.mDataModel = bzVar;
            if (bzVar != null && ActivityChooserView.this.isShown()) {
                bzVar.registerObserver(ActivityChooserView.this.ˎ);
            }
            notifyDataSetChanged();
        }

        public int getItemViewType(int i) {
            if (this.mShowFooterView && i == getCount() - 1) {
                return 1;
            }
            return 0;
        }

        public int getViewTypeCount() {
            return 3;
        }

        public int getCount() {
            int ˏ = this.mDataModel.ˏ();
            if (!(this.mShowDefaultActivity || this.mDataModel.ॱ() == null)) {
                ˏ--;
            }
            ˏ = Math.min(ˏ, this.mMaxActivityCount);
            if (this.mShowFooterView) {
                return ˏ + 1;
            }
            return ˏ;
        }

        public Object getItem(int i) {
            switch (getItemViewType(i)) {
                case 0:
                    if (!(this.mShowDefaultActivity || this.mDataModel.ॱ() == null)) {
                        i++;
                    }
                    return this.mDataModel.ˊ(i);
                case 1:
                    return null;
                default:
                    throw new IllegalArgumentException();
            }
        }

        public long getItemId(int i) {
            return (long) i;
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            switch (getItemViewType(i)) {
                case 0:
                    if (view == null || view.getId() != ah$i.ʾ) {
                        view = LayoutInflater.from(ActivityChooserView.this.getContext()).inflate(ah$j.ʽ, viewGroup, false);
                    }
                    PackageManager packageManager = ActivityChooserView.this.getContext().getPackageManager();
                    ResolveInfo resolveInfo = (ResolveInfo) getItem(i);
                    ((ImageView) view.findViewById(ah$i.ʼॱ)).setImageDrawable(resolveInfo.loadIcon(packageManager));
                    ((TextView) view.findViewById(ah$i.ـ)).setText(resolveInfo.loadLabel(packageManager));
                    if (this.mShowDefaultActivity && i == 0 && this.mHighlightDefaultActivity) {
                        view.setActivated(true);
                        return view;
                    }
                    view.setActivated(false);
                    return view;
                case 1:
                    if (view != null && view.getId() == 1) {
                        return view;
                    }
                    view = LayoutInflater.from(ActivityChooserView.this.getContext()).inflate(ah$j.ʽ, viewGroup, false);
                    view.setId(1);
                    ((TextView) view.findViewById(ah$i.ـ)).setText(ActivityChooserView.this.getContext().getString(ah$h.ˊ));
                    return view;
                default:
                    throw new IllegalArgumentException();
            }
        }

        public int measureContentWidth() {
            int i = 0;
            int i2 = this.mMaxActivityCount;
            this.mMaxActivityCount = MAX_ACTIVITY_COUNT_UNLIMITED;
            int makeMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
            int makeMeasureSpec2 = MeasureSpec.makeMeasureSpec(0, 0);
            int count = getCount();
            View view = null;
            int i3 = 0;
            while (i < count) {
                view = getView(i, view, null);
                view.measure(makeMeasureSpec, makeMeasureSpec2);
                i3 = Math.max(i3, view.getMeasuredWidth());
                i++;
            }
            this.mMaxActivityCount = i2;
            return i3;
        }

        public void setMaxActivityCount(int i) {
            if (this.mMaxActivityCount != i) {
                this.mMaxActivityCount = i;
                notifyDataSetChanged();
            }
        }

        public ResolveInfo getDefaultActivity() {
            return this.mDataModel.ॱ();
        }

        public void setShowFooterView(boolean z) {
            if (this.mShowFooterView != z) {
                this.mShowFooterView = z;
                notifyDataSetChanged();
            }
        }

        public int getActivityCount() {
            return this.mDataModel.ˏ();
        }

        public int getHistorySize() {
            return this.mDataModel.ˋ();
        }

        public bz getDataModel() {
            return this.mDataModel;
        }

        public void setShowDefaultActivity(boolean z, boolean z2) {
            if (this.mShowDefaultActivity != z || this.mHighlightDefaultActivity != z2) {
                this.mShowDefaultActivity = z;
                this.mHighlightDefaultActivity = z2;
                notifyDataSetChanged();
            }
        }

        public boolean getShowDefaultActivity() {
            return this.mShowDefaultActivity;
        }
    }

    @RestrictTo({d.LIBRARY_GROUP})
    public static class InnerLayout extends LinearLayout {
        private static final int[] ˏ = new int[]{16842964};

        public InnerLayout(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            cw ˊ = cw.ˊ(context, attributeSet, ˏ);
            setBackgroundDrawable(ˊ.ˋ(0));
            ˊ.ॱ();
        }
    }

    class c implements OnItemClickListener, OnClickListener, OnLongClickListener, OnDismissListener {
        final /* synthetic */ ActivityChooserView ˎ;

        c(ActivityChooserView activityChooserView) {
            this.ˎ = activityChooserView;
        }

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            switch (((ActivityChooserViewAdapter) adapterView.getAdapter()).getItemViewType(i)) {
                case 0:
                    this.ˎ.ˏ();
                    if (!this.ˎ.ʽ) {
                        if (!this.ˎ.ॱ.getShowDefaultActivity()) {
                            i++;
                        }
                        Intent ॱ = this.ˎ.ॱ.getDataModel().ॱ(i);
                        if (ॱ != null) {
                            ॱ.addFlags(524288);
                            this.ˎ.getContext().startActivity(ॱ);
                            return;
                        }
                        return;
                    } else if (i > 0) {
                        this.ˎ.ॱ.getDataModel().ˎ(i);
                        return;
                    } else {
                        return;
                    }
                case 1:
                    this.ˎ.ˎ(ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED);
                    return;
                default:
                    throw new IllegalArgumentException();
            }
        }

        public void onClick(View view) {
            if (view == this.ˎ.ˊ) {
                this.ˎ.ˏ();
                Intent ॱ = this.ˎ.ॱ.getDataModel().ॱ(this.ˎ.ॱ.getDataModel().ˊ(this.ˎ.ॱ.getDefaultActivity()));
                if (ॱ != null) {
                    ॱ.addFlags(524288);
                    this.ˎ.getContext().startActivity(ॱ);
                }
            } else if (view == this.ˎ.ˏ) {
                this.ˎ.ʽ = false;
                this.ˎ.ˎ(this.ˎ.ʼ);
            } else {
                throw new IllegalArgumentException();
            }
        }

        public boolean onLongClick(View view) {
            if (view == this.ˎ.ˊ) {
                if (this.ˎ.ॱ.getCount() > 0) {
                    this.ˎ.ʽ = true;
                    this.ˎ.ˎ(this.ˎ.ʼ);
                }
                return true;
            }
            throw new IllegalArgumentException();
        }

        public void onDismiss() {
            ˎ();
            if (this.ˎ.ˋ != null) {
                this.ˎ.ˋ.ˏ(false);
            }
        }

        private void ˎ() {
            if (this.ˎ.ॱॱ != null) {
                this.ˎ.ॱॱ.onDismiss();
            }
        }
    }

    public ActivityChooserView(Context context) {
        this(context, null);
    }

    public ActivityChooserView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public ActivityChooserView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ˎ = new DataSetObserver(this) {
            final /* synthetic */ ActivityChooserView ˎ;

            {
                this.ˎ = r1;
            }

            public void onChanged() {
                super.onChanged();
                this.ˎ.ॱ.notifyDataSetChanged();
            }

            public void onInvalidated() {
                super.onInvalidated();
                this.ˎ.ॱ.notifyDataSetInvalidated();
            }
        };
        this.ͺ = new OnGlobalLayoutListener(this) {
            final /* synthetic */ ActivityChooserView ॱ;

            {
                this.ॱ = r1;
            }

            public void onGlobalLayout() {
                if (!this.ॱ.ॱ()) {
                    return;
                }
                if (this.ॱ.isShown()) {
                    this.ॱ.ˎ().ˊ();
                    if (this.ॱ.ˋ != null) {
                        this.ॱ.ˋ.ˏ(true);
                        return;
                    }
                    return;
                }
                this.ॱ.ˎ().ˋ();
            }
        };
        this.ʼ = 4;
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, g.ˎˎ, i, 0);
        this.ʼ = obtainStyledAttributes.getInt(g.ˋᐝ, 4);
        Drawable drawable = obtainStyledAttributes.getDrawable(g.ˋˋ);
        obtainStyledAttributes.recycle();
        LayoutInflater.from(getContext()).inflate(ah$j.ॱ, this, true);
        this.ʻ = new c(this);
        this.ᐝ = findViewById(ah$i.ʽ);
        this.ॱˊ = this.ᐝ.getBackground();
        this.ˊ = (FrameLayout) findViewById(ah$i.ॱᐝ);
        this.ˊ.setOnClickListener(this.ʻ);
        this.ˊ.setOnLongClickListener(this.ʻ);
        this.ˋॱ = (ImageView) this.ˊ.findViewById(ah$i.ˈ);
        FrameLayout frameLayout = (FrameLayout) findViewById(ah$i.ॱˎ);
        frameLayout.setOnClickListener(this.ʻ);
        frameLayout.setAccessibilityDelegate(new AccessibilityDelegate(this) {
            final /* synthetic */ ActivityChooserView ˋ;

            {
                this.ˋ = r1;
            }

            public void onInitializeAccessibilityNodeInfo(View view, AccessibilityNodeInfo accessibilityNodeInfo) {
                super.onInitializeAccessibilityNodeInfo(view, accessibilityNodeInfo);
                in.ॱ(accessibilityNodeInfo).ˊॱ(true);
            }
        });
        frameLayout.setOnTouchListener(new cm(this, frameLayout) {
            final /* synthetic */ ActivityChooserView ˎ;

            public bt ˎ() {
                return this.ˎ.ˎ();
            }

            public boolean ˏ() {
                this.ˎ.ˊ();
                return true;
            }

            public boolean ॱ() {
                this.ˎ.ˏ();
                return true;
            }
        });
        this.ˏ = frameLayout;
        this.ˊॱ = (ImageView) frameLayout.findViewById(ah$i.ˈ);
        this.ˊॱ.setImageDrawable(drawable);
        this.ॱ = new ActivityChooserViewAdapter();
        this.ॱ.registerDataSetObserver(new DataSetObserver(this) {
            final /* synthetic */ ActivityChooserView ॱ;

            {
                this.ॱ = r1;
            }

            public void onChanged() {
                super.onChanged();
                this.ॱ.ˋ();
            }
        });
        Resources resources = context.getResources();
        this.ˏॱ = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(e.ˋ));
    }

    public void setActivityChooserModel(bz bzVar) {
        this.ॱ.setDataModel(bzVar);
        if (ॱ()) {
            ˏ();
            ˊ();
        }
    }

    public void setExpandActivityOverflowButtonDrawable(Drawable drawable) {
        this.ˊॱ.setImageDrawable(drawable);
    }

    public void setExpandActivityOverflowButtonContentDescription(int i) {
        this.ˊॱ.setContentDescription(getContext().getString(i));
    }

    @RestrictTo({d.LIBRARY_GROUP})
    public void setProvider(hd hdVar) {
        this.ˋ = hdVar;
    }

    public boolean ˊ() {
        if (ॱ() || !this.ʻॱ) {
            return false;
        }
        this.ʽ = false;
        ˎ(this.ʼ);
        return true;
    }

    void ˎ(int i) {
        if (this.ॱ.getDataModel() == null) {
            throw new IllegalStateException("No data model. Did you call #setDataModel?");
        }
        getViewTreeObserver().addOnGlobalLayoutListener(this.ͺ);
        boolean z = this.ˊ.getVisibility() == 0;
        int activityCount = this.ॱ.getActivityCount();
        int i2;
        if (z) {
            i2 = 1;
        } else {
            i2 = 0;
        }
        if (i == ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED || activityCount <= r3 + i) {
            this.ॱ.setShowFooterView(false);
            this.ॱ.setMaxActivityCount(i);
        } else {
            this.ॱ.setShowFooterView(true);
            this.ॱ.setMaxActivityCount(i - 1);
        }
        ListPopupWindow ˎ = ˎ();
        if (!ˎ.ˎ()) {
            if (this.ʽ || !z) {
                this.ॱ.setShowDefaultActivity(true, z);
            } else {
                this.ॱ.setShowDefaultActivity(false, false);
            }
            ˎ.ʼ(Math.min(this.ॱ.measureContentWidth(), this.ˏॱ));
            ˎ.ˊ();
            if (this.ˋ != null) {
                this.ˋ.ˏ(true);
            }
            ˎ.ᐝ().setContentDescription(getContext().getString(ah$h.ˏ));
            ˎ.ᐝ().setSelector(new ColorDrawable(0));
        }
    }

    public boolean ˏ() {
        if (ॱ()) {
            ˎ().ˋ();
            ViewTreeObserver viewTreeObserver = getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.removeGlobalOnLayoutListener(this.ͺ);
            }
        }
        return true;
    }

    public boolean ॱ() {
        return ˎ().ˎ();
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        bz dataModel = this.ॱ.getDataModel();
        if (dataModel != null) {
            dataModel.registerObserver(this.ˎ);
        }
        this.ʻॱ = true;
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        bz dataModel = this.ॱ.getDataModel();
        if (dataModel != null) {
            dataModel.unregisterObserver(this.ˎ);
        }
        ViewTreeObserver viewTreeObserver = getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.removeGlobalOnLayoutListener(this.ͺ);
        }
        if (ॱ()) {
            ˏ();
        }
        this.ʻॱ = false;
    }

    protected void onMeasure(int i, int i2) {
        View view = this.ᐝ;
        if (this.ˊ.getVisibility() != 0) {
            i2 = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(i2), 1073741824);
        }
        measureChild(view, i, i2);
        setMeasuredDimension(view.getMeasuredWidth(), view.getMeasuredHeight());
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        this.ᐝ.layout(0, 0, i3 - i, i4 - i2);
        if (!ॱ()) {
            ˏ();
        }
    }

    public void setOnDismissListener(OnDismissListener onDismissListener) {
        this.ॱॱ = onDismissListener;
    }

    public void setInitialActivityCount(int i) {
        this.ʼ = i;
    }

    public void setDefaultActionButtonContentDescription(int i) {
        this.ॱˋ = i;
    }

    ListPopupWindow ˎ() {
        if (this.ᐝॱ == null) {
            this.ᐝॱ = new ListPopupWindow(getContext());
            this.ᐝॱ.ˏ(this.ॱ);
            this.ᐝॱ.ˏ((View) this);
            this.ᐝॱ.ˎ(true);
            this.ᐝॱ.ˊ(this.ʻ);
            this.ᐝॱ.ॱ(this.ʻ);
        }
        return this.ᐝॱ;
    }

    void ˋ() {
        if (this.ॱ.getCount() > 0) {
            this.ˏ.setEnabled(true);
        } else {
            this.ˏ.setEnabled(false);
        }
        int activityCount = this.ॱ.getActivityCount();
        int historySize = this.ॱ.getHistorySize();
        if (activityCount == 1 || (activityCount > 1 && historySize > 0)) {
            this.ˊ.setVisibility(0);
            ResolveInfo defaultActivity = this.ॱ.getDefaultActivity();
            PackageManager packageManager = getContext().getPackageManager();
            this.ˋॱ.setImageDrawable(defaultActivity.loadIcon(packageManager));
            if (this.ॱˋ != 0) {
                CharSequence loadLabel = defaultActivity.loadLabel(packageManager);
                this.ˊ.setContentDescription(getContext().getString(this.ॱˋ, new Object[]{loadLabel}));
            }
        } else {
            this.ˊ.setVisibility(8);
        }
        if (this.ˊ.getVisibility() == 0) {
            this.ᐝ.setBackgroundDrawable(this.ॱˊ);
        } else {
            this.ᐝ.setBackgroundDrawable(null);
        }
    }
}
